import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AudioFile {
  final String id;
  final String title;
  final String date;
  final String time;
  final String duration;
  bool isSelected;

  AudioFile({
    required this.id,
    required this.title,
    required this.date,
    required this.time,
    required this.duration,
    this.isSelected = false,
  });
}

class AudioSelectionDialog extends StatefulWidget {
  final Function(AudioFile) onSelect;

  const AudioSelectionDialog({
    Key? key,
    required this.onSelect,
  }) : super(key: key);

  @override
  State<AudioSelectionDialog> createState() => _AudioSelectionDialogState();
}

class _AudioSelectionDialogState extends State<AudioSelectionDialog> {
  final RxList<AudioFile> audioFiles = <AudioFile>[
    AudioFile(
      id: '1',
      title: '显示录音转写总结后的文件标题',
      date: '2025/2/21',
      time: '17:34',
      duration: '34:38',
      isSelected: true,
    ),
    AudioFile(
      id: '2',
      title: '显示录音转写总结后的文件标题',
      date: '2025/2/21',
      time: '17:34',
      duration: '34:38',
      isSelected: false,
    ),
  ].obs;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 500.h,
      decoration: BoxDecoration(
        color: Color(0xFFF7F9FF),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.r),
          topRight: Radius.circular(20.r),
        ),
      ),
      child: Column(
        children: [
          _buildHeader(),
          Expanded(
            child: _buildAudioList(),
          ),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
      // decoration: BoxDecoration(
      //   border: Border(
      //     bottom: BorderSide(
      //       color: Colors.grey.shade200,
      //       width: 1.h,
      //     ),
      //   ),
      // ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            '录音文档',
            style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w500,
              color: Colors.black,
            ),
          ),
          GestureDetector(
            onTap: () {
              // 确认选择
              final selectedFile = audioFiles.firstWhere(
                (file) => file.isSelected,
                orElse: () => audioFiles.first,
              );
              widget.onSelect(selectedFile);
              Get.back();
            },
            child: Icon(
              Icons.check,
              color: Colors.blue,
              size: 24.r,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAudioList() {
    return ListView.builder(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 10.h),
      itemCount: audioFiles.length,
      itemBuilder: (context, index) {
        return _buildAudioItem(audioFiles[index]);
      },
    );
  }

  Widget _buildAudioItem(AudioFile file) {
    return GestureDetector(
      onTap: () {
        setState(() {
          for (var item in audioFiles) {
            item.isSelected = item.id == file.id;
          }
        });
      },
      child: Container(
        margin: EdgeInsets.symmetric(vertical: 10.h),
        padding: EdgeInsets.all(16.w),
        decoration: BoxDecoration(
          color: Color(0xFFEEEEEF),
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    file.title,
                    style: TextStyle(
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500,
                      color: Colors.black87,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 8.h),
                  Text(
                    '${file.date}  ${file.time}  ${file.duration}',
                    style: TextStyle(
                      fontSize: 10.sp,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(width: 10.w),
            Container(
              width: 24.w,
              height: 24.h,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: file.isSelected ? Colors.blue : Colors.transparent,
                border: Border.all(
                  color: file.isSelected ? Colors.blue : Colors.grey,
                  width: 2.w,
                ),
              ),
              child: file.isSelected
                  ? Icon(
                      Icons.check,
                      color: Colors.white,
                      size: 16.r,
                    )
                  : null,
            ),
          ],
        ),
      ),
    );
  }
} 